package com.neothefox.romashka;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class RomashkaView extends View {

	private Paint circlePaint;
	private Paint textPaint;
	private int Radius;
	private int xOffset, yOffset;
	private int radius = 40;
	private int circleSector;
	private int textSector;
	private int mod;
	final private int txtSize = 18;
	private int alpha;
	
	private Layout layout;
	
	public RomashkaView(Context context) {
        super(context);
        initRomashkaView();
	}

	public RomashkaView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initRomashkaView();
	}

	public RomashkaView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initRomashkaView();
		}
	
	void initRomashkaView()
	{
		setFocusable(false);
		
	    circleSector = -1;
	    textSector = -1;
	    mod = 1;
	    alpha = 0;
	    xOffset = 200;
	    yOffset = 200;
	    
	    layout = new Layout();
	    layout.setMain();
	    
		circlePaint = new Paint();
		textPaint = new Paint();
		
		circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint.setColor(Color.GRAY);
		circlePaint.setStrokeWidth(1);
		circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
		circlePaint.setAlpha(alpha);
		
		textPaint.setColor(Color.BLACK);
		textPaint.setAntiAlias(true);
		textPaint.setStyle(Style.FILL);
		textPaint.setTextSize(txtSize);
		
		Radius = 100;
		
	}
	
	@Override
    protected void onDraw(Canvas canvas) {
			
            // Draw the background
			for(int i=0; i < 8; i++)
			{
				double angle = (i*45*Math.PI)/180;
				int x = (int)(Math.cos(angle) * Radius);
				int y = (int)(Math.sin(angle) * Radius);
				if(i != circleSector)
				{	 
					textPaint.setColor(Color.BLACK);
					textPaint.setTextSize(txtSize);
					radius = 40;
					canvas.drawCircle(x+xOffset, y+yOffset, radius, circlePaint);
				}
				if(i == circleSector)
				{
					radius = 60;
					textPaint.setTextSize(txtSize + 5);
					canvas.drawCircle(x+xOffset, y+yOffset, radius, circlePaint);
				}
				for(int j = 0; j < 4; j++)
				{
					if(j == textSector && i == circleSector)
					{
						//textPaint.setColor(Color.RED);
					}
					else
					{
						textPaint.setColor(Color.BLACK);
					}
					
					double tangle = (j*90*Math.PI)/180;
					int tx = (int)(Math.cos(tangle) * radius/2) + x;
					int ty = (int)(Math.sin(tangle) * radius/2) + y;
					
					
					canvas.drawText(layout.charmap[i][mod][j],tx+xOffset,ty+yOffset,textPaint);
				}
			}
			
            canvas.save();
    }
	
	public void setCircleSector(int x, int y)
	{
		circleSector = selectSector(x, y);
	}
	
	public int getCircleSector()
	{
		return circleSector;
	}
	
	public int getTextSector()
	{
		return textSector;
	}
	
	public void setTextSector(int sect)
	{
		textSector = sect;
	}
	
	public void setMod(int m)
	{
		mod = m;
	}
	
	public void setAlpha(int a)
	{
		alpha = a;
		circlePaint.setAlpha(alpha);
	}
	
	public void setLayout(Layout.TYPE version)
	{
		if(version == Layout.TYPE.MAIN)
		{
			layout.setMain(); 
		}
		if(version == Layout.TYPE.NUMERIC)
		{
			layout.setNumeric();
		}
	}
	
	public void setUserLayout(String path) throws IOException
	{
		layout.setUser(new File(path));
	}
	
	public void setOffset(int x, int y)
	{
		xOffset = x;
		yOffset = y;
	}
	
	private int selectSector(int x, int y)
	{
		int sector = -1;
		if(x == 1)
		{
			if(y == 1)
				sector = 1;
			else if(y == 0)
				sector = 2;
			else if(y == -1)
				sector = 3;
		}
		else if (x == 0)
		{
			if(y == 1)
				sector = 0;
			else if(y == 0)
				sector = -1;
			else if(y == -1)
				sector = 4;
		}
		else if(x == -1)
		{
			if(y == 1)
				sector = 7;
			else if(y == 0)
				sector = 6;
			else if(y == -1)
				sector = 5;
		}
		return sector;
	}
}
